package com.zs.grain.ui

import android.os.Bundle
import android.os.Handler
import android.text.Editable
import android.text.TextWatcher
import android.widget.SeekBar
import com.zs.grain.*
import com.zs.grain.databinding.*
import com.xnkj1688.semplibrary.mvvm.*
import android.os.Looper
import android.os.Message
import android.widget.Toast
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Observer
import com.jeremyliao.liveeventbus.LiveEventBus


class DurationDialog : BaseMvvmDialog<DialogDurationBinding, DurationViewModel>(),
    SeekBar.OnSeekBarChangeListener, TextWatcher {

    override val layoutId = R.layout.dialog_duration

    override fun initViewModel(binding: DialogDurationBinding) {
        binding.vm = this.mViewModel
        binding.view = this
        //binding.btnConfirm.
        binding.seekBar.setOnSeekBarChangeListener(this)
        binding.etValue.addTextChangedListener(this)
        LiveEventBus.get("KeyEvent").observe(this, Observer{
            mViewModel.onClickConfirm()
        })
    }

    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
        if (fromUser) {
            mViewModel.value.value = progress + (mViewModel.min.value ?: 0)
        }
    }

    override fun onStartTrackingTouch(seekBar: SeekBar?) {
    }

    override fun onStopTrackingTouch(seekBar: SeekBar?) {
    }

    override fun afterTextChanged(s: Editable?) {
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        if (s?.length ?: 0 > 0) {
            mViewModel.value.value = s.toString().toInt()
            mBinding.etValue.setSelection(s.toString().length)
        } else {
            mViewModel.value.value = null
        }
    }

    internal lateinit var mHandler: Handler
    var value: Int? = null
        private set

    override fun initArguments() {
        var args = arguments!!
        var title = args.getString("Title")
        var min = args.getInt("Min")
        var max = args.getInt("Max")
        var value = args.getInt("Value")
        mViewModel.init(title, min, max, value)
    }

    override fun initView() {
        this.isCancelable = false
    }

    override fun onUiNotify(name: String, bundle: Bundle) {
        super.onUiNotify(name, bundle)
        when (name) {
            "Confirm" -> {
                if(mViewModel.isDurationTesting) {
                    mBinding.tvMax.setText("循环测试已开始")
                    this.value = 4
                    //Toast.makeText(context,"循环测试已开始",Toast.LENGTH_SHORT)
                }else {
                    this.value = 1 //1 开始   2 中止   3取消    4进行中
                    //Toast.makeText(context,"开始循环测试",Toast.LENGTH_SHORT)
                }
                //mBinding.btnConfirm
                //this.value = Math.max(mViewModel.value.value ?: 0, mViewModel.min.value ?: 0)
                //this.value = Math.min(this.value ?: 0, mViewModel.max.value ?: 0)
                //mHandler.sendEmptyMessage(0);//dismiss dialog
            }
            "Cancel" -> {
                mHandler.sendEmptyMessage(0)
            };
            "Stop" -> {
                mBinding.tvMax.setText("循环测试已中止")
                //Toast.makeText(context,"循环测试已中止",Toast.LENGTH_SHORT)
                //this.btnConfirm.setEnable = false
                //mHandler.sendEmptyMessage(0)
            };
        }
    }

    fun showDialog(manager: FragmentManager, tag: String): Int? {
        mHandler = object : Handler() {
            override fun handleMessage(mesg: Message) {
                throw RuntimeException()
            }
        }
        super.show(manager, tag)
        try {
            Looper.getMainLooper()
            Looper.loop()
        } catch (e: Exception) {
        }
        this.dismiss()
        return this.value
    }

    companion object {
        val TAG = "DurationDialog"
        fun newInstance(title: String, min: Int, max: Int, value: Int): DurationDialog {
            val dlg = DurationDialog()
            var args = Bundle();
            args.putString("Title", title)
            args.putInt("Min", min)
            args.putInt("Max", max)
            args.putInt("Value", value)
//            args.putInt("Min", min)
//            args.putInt("Max", max)
//            args.putInt("Value", value)
            dlg.arguments = args
            return dlg
        }

        fun setString(content:String){
            //.tvMax.setText(content)
            println("kkk:"+content)
        }

        fun getValue():Int{
            return 1//this.value
        }
    }
}
